package com.hqd.jjvm.classformat.constantpool;

import com.hqd.jjvm.classformat.ClassFile;

import java.util.LinkedList;
import java.util.List;

/**
 * 常量池工厂
 *
 * @author hqd
 */
public class ConstantPoolFactory {
    public static List<ConstantPool> newConstantPool(ConstantInfoType tag, ClassFile classFile) {
        List<ConstantPool> constantPoolList = new LinkedList<>();
        switch (tag) {
            case CONSTANT_UTF8:
                constantPoolList.add(new ConstantUtf8Info(tag, classFile));
                break;
            case CONSTANT_INTEGER:
                constantPoolList.add(new ConstantIntegerInfo(tag, classFile));
                break;
            case CONSTANT_FLOAT:
                constantPoolList.add(new ConstantFloatInfo(tag, classFile));
                break;
            case CONSTANT_FIELDREF:
                constantPoolList.add(new ConstantFieldRefInfo(tag, classFile));
                break;
            case CONSTANT_METHODREF:
                constantPoolList.add(new ConstantMethodRefInfo(tag, classFile));
                break;
            case CONSTANT_INTERFACEMETHODREF:
                constantPoolList.add(new ConstantInterfaceMethodRefInfo(tag, classFile));
                break;
            case CONSTANT_NAMEANDTYPE:
                constantPoolList.add(new ConstantNameAndTypeInfo(tag, classFile));
                break;
            case CONSTANT_INVOKEDYNAMIC:
                constantPoolList.add(new ConstantInvokedynamicInfo(tag, classFile));
                break;
            case CONSTANT_METHODHANDLE:
                constantPoolList.add(new ConstantMethodhandleInfo(tag, classFile));
                break;
            case CONSTANT_LONG:
                constantPoolList.add(new ConstantLongInfo(tag, classFile));
                constantPoolList.add(new ConstantPool(""));
                break;
            case CONSTANT_DOUBLE:
                constantPoolList.add(new ConstantDoubleInfo(tag, classFile));
                constantPoolList.add(new ConstantPool(""));
                break;
            case CONSTANT_CLASS:
                constantPoolList.add(new ConstantClassInfo(tag, classFile));
                break;
            case CONSTANT_STRING:
                constantPoolList.add(new ConstantStringInfo(tag, classFile));
                break;
            case CONSTANT_METHODTYPE:
                constantPoolList.add(new ConstantMethodType(tag, classFile));
                break;
            default:
                throw new ClassFormatError("无效常量池类型");
        }
        return constantPoolList;
    }
}
